const path = require('path');

function resolve(dir) {
  return path.join(__dirname, dir);
}

const origin = {
  // dev: 'http://10.240.206.100:8080', // (dev) 凌志伟本地
  dev: 'http://10.229.255.52:8090', // (uat) 使用网关
  test: 'http://10.1.20.15:9096', // (test) 使用网关 (开发时请勿修改测试环境数据库)
};

module.exports = {
  // lintOnSave: process.env.NODE_ENV === 'production' ? true : 'error', // Todo：警告：不要修改和注释该行代码！！！
  // baseUrl: '/',
  devServer: {
    port: 9091,
    proxy: {
      '/api': {
        target: origin.dev,
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/api': '',
        },
      },
    },
  },
  runtimeCompiler: true,
  productionSourceMap: true, // Todo: 开发和测试暂时为打开状态，上生产时应该关闭此功能，切记
  chainWebpack: config => {
    console.log('\n === 当前构建sourceMap处于打开状态,上生产时应该关闭此功能（!请勿去掉该注释）\n');
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@proposal', resolve('src/modules/proposal'))
      .set('@config', resolve('src/modules/config'))
      .set('@assist', resolve('src/modules/assist'))
      .set('@tools', resolve('src/modules/tools'))
      .set('@mock', resolve('public/mock'));
  },
  configureWebpack: config => {
    config.entry = ['babel-polyfill', './src/main.js'];
    if (process.env.NODE_ENV === 'production') {
      config.optimization = {
        runtimeChunk: {
          name: 'manifest',
        },
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            libs: {
              name: 'chunk-libs',
              test: /[\\/]node_modules[\\/]/,
              priority: 10,
              chunks: 'initial', // 只打包初始时依赖的第三方
            },
            lodash: {
              name: 'chunk-lodash',
              test: /[\\/]node_modules[\\/]lodash[\\/]/,
              priority: 20,
            },
            echarts: {
              name: 'chunk-echarts',
              test: /[\\/]node_modules[\\/]echarts[\\/]/,
              priority: 20,
            },
            elementUI: {
              name: 'chunk-elementui', // 单独将 elementUI 拆包
              priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
              test: /[\\/]node_modules[\\/]element-ui[\\/]/,
            },
            commons: {
              name: 'chunk-commons',
              test: resolve('src/components'), // 可自定义拓展你的规则
              minChunks: 3, // 最小公用次数
              priority: 5,
              reuseExistingChunk: true,
            },
          },
        },
      };
    }
  },
};
